package nix.web.action;

import nix.domain.SaleVisit;
import nix.domain.User;
import nix.service.SaleVisitService;
import nix.utils.PageBean;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class SaleVisitAction extends ActionSupport implements ModelDriven<SaleVisit> {

	private SaleVisitService saleVisitService;
	
	private SaleVisit saleVisit = new SaleVisit();
	private Integer currentPage;
	private Integer pageSize;
	
	public String add() throws Exception {
		
		User user = (User) ActionContext.getContext().getSession().get("session_user");
		saleVisit.setUser(user);
		saleVisitService.save(saleVisit);		
		return "toList";
	}
	
	public String list() throws Exception {

		DetachedCriteria dc = DetachedCriteria.forClass(SaleVisit.class);
		if (this.saleVisit.getCustomer() != null && this.saleVisit.getCustomer().getCust_id() != null) {
			dc.add(Restrictions.eq("customer.cust_id", this.saleVisit.getCustomer().getCust_id()));
		}
		PageBean pb = saleVisitService.getPageBean(dc, currentPage, pageSize);
		ActionContext.getContext().put("pageBean", pb);
		return "list";
	}
	
	public String toEdit() throws Exception {

		SaleVisit sv = saleVisitService.getById(saleVisit.getVisit_id());
		ActionContext.getContext().put("saleVisit", sv);
		return "edit";
	}
	
	public void setSaleVisitService(SaleVisitService saleVisitService) {
		this.saleVisitService = saleVisitService;
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	@Override
	public SaleVisit getModel() {
		return this.saleVisit;
	}

}
